package com.itheima.service;

import com.itheima.dao.BaseStudentDao;
import com.itheima.domain.Option;
import com.itheima.domain.Student;
import com.itheima.factory.StudentDaoFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.Socket;
import java.sql.SQLException;
import java.util.List;

public class StudentService implements Runnable {

    private static final Logger LOGGER = LoggerFactory.getLogger(StudentService.class);

    private BaseStudentDao dao = StudentDaoFactory.getStudentDao();
    private Socket socket;

    public StudentService(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            //1. 接收客户端传递过来的报文数据
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String allMsg = br.readLine();
            //System.out.println("接收到来自客户端" + socket.getLocalAddress() + " 的请求: " + allMsg);
            LOGGER.info("接收到来自客户端" + socket.getLocalAddress() + " 的请求: " + allMsg);
            //2. 解析报文
            String[] split = allMsg.split("&");
            Option option = Enum.valueOf(Option.class, split[0]);
            String optionMsg = split[1];

            //3. 根据操作指令进行功能路由
            switch (option){
                case ADD_STUDENT:
                    addStudent(optionMsg);
                    break;
                case DELECT_STUDENT:
                    delectStudent(optionMsg);
                    break;
                case UPDATE_STUDENT:
                    upadeStudent(optionMsg);
                    break;
                case FINDALL_STUDENT:
                    findAllStudent();
                    break;
            }


        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //服务器端-修改学生功能
    private void upadeStudent(String optionMsg) {
        LOGGER.info("修改学生信息,数据:" + optionMsg);
        //解析业务数据,封装成学生对象
        String[] split = optionMsg.split(",");
        Student student = new Student(split[0],split[1],split[2],split[3]);
        //调用dao层更新功能
        boolean res = dao.updateStudent(split[0], student);
        //响应结果给客户端
        String resultInfo = res ? "修改成功" : "修改失败";
        try {
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write(resultInfo);
            bw.newLine();
            bw.flush();

            bw.close();
            socket.close();
            LOGGER.info("修改完成,响应客户端数据:" + resultInfo);
        } catch (IOException e) {
            //e.printStackTrace();
            LOGGER.error("修改时,服务器异常:" + e.getMessage());
        }
    }

    //服务器端-删除学生功能
    private void delectStudent(String optionMsg){
        LOGGER.info("删除学生信息,数据:" + optionMsg);
        //1.调用dao层删除操作
        boolean res = dao.deleteStudent(optionMsg);
        //响应结果给客户端
        String resultInfo = res ? "删除成功" : "删除失败";
        try {
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write(resultInfo);
            bw.newLine();
            bw.flush();

            bw.close();
            socket.close();
            LOGGER.info("删除完成,响应客户端数据:" + resultInfo);
        } catch (IOException e) {
            //e.printStackTrace();
            LOGGER.error("删除中,服务器异常:" + e.getMessage());
        }
    }

    //服务器端-查看学生功能
    private void findAllStudent() throws SQLException {
        LOGGER.info("查看学生信息.");
        //1. 调用dao,拿到系统数据
        List<Student> list = dao.findAllStudent();

        try {
            //2. 通过socket将整个集合对象写给客户端
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(list);
            oos.close();
            socket.close();
            LOGGER.info("查看完成,响应客户端数据:" + list);
        } catch (IOException e) {
            //e.printStackTrace();
            LOGGER.error("删除中,服务器异常:" + e.getMessage());
        }
    }

    //服务器端-添加学生功能
    private void addStudent(String optionMsg){
        LOGGER.info("添加学生信息,数据:" + optionMsg);
        //1.解析业务数据,封装学生对象
        String[] split = optionMsg.split(",");
        Student stu = new Student(split[0],split[1],split[2],split[3]);
        //2. 调用dao,进行数据存储
        boolean res = dao.addStudent(stu);

        //3. 响应结果给客户端
        String resultInfo = res == true ? "添加成功!": "该学号已存在,添加失败!";
        try {
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write(resultInfo);
            bw.newLine();
            bw.flush();

            bw.close();
            socket.close();
            LOGGER.info("添加完成,响应客户端数据:" + resultInfo);
        } catch (IOException e) {
            //e.printStackTrace();
            LOGGER.error("添加中,服务器异常:" + e.getMessage());
        }
    }
}
